using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input.Touch;

namespace Pokemon
{
    class Menu: IAnimation
    {
#region Attribute
        private const int MenuEntryPadding = 25;        
        private string _name;
        private List<Entry> _menuEntries = new List<Entry>();
        private GameScreen _owner;
        
#endregion

        public Menu(GameScreen gameScreen, string name) 
        {
            Owner = gameScreen;
            _name = name;
        }

        public List<Entry> MenuEntries
        {
            get { return _menuEntries; }
            set { _menuEntries = value; }
        }

        public GameScreen Owner
        {
            get { return _owner; }
            set { _owner = value; }
        }

        public void Add(Entry entry)
        {
            _menuEntries.Add(entry);
        }
        public void Add(Entry entry, int index)
        {
            _menuEntries.Insert(index, entry);
        }

        public void Draw(SpriteBatch spriteBatch, SpriteFont font)
        {
            if (_isStartup)
            {
                Animation(spriteBatch);
            }       
            foreach (var menuEntry in _menuEntries)
            {
                menuEntry.Draw(spriteBatch, font);
            }
        }

        public void Update()
        {
            // look for any taps that occurred and select any entries that were tapped
            TouchCollection tc = TouchPanel.GetState();
            foreach (var touch in tc)
            {
                if (touch.State == TouchLocationState.Pressed)
                {
                    Point pressedPoint = new Point((int)touch.Position.X, (int)touch.Position.Y);
                    for (var i = 0; i <_menuEntries.Count; i++)
                    {
                        if (GetMenuEntryHitBounds(_menuEntries[i]).Contains(pressedPoint))
                            OnSelectEntry(i);
                    }
                }

            }
        }

        /// <summary>
        /// Handler for when the user has chosen a menu entry.
        /// </summary>
        protected virtual void OnSelectEntry(int entryIndex)
        {
            _menuEntries[entryIndex].OnSelected();
        }


        /// <summary>
        /// Handler for when the user has cancelled the menu.
        /// </summary>
        protected virtual void OnCancel()
        {
           Owner.Exit();
        }

        /// <summary>
        /// Allows the screen to create the hit bounds for a particular menu entry.
        /// </summary>
        protected virtual Rectangle GetMenuEntryHitBounds(Entry entry)
        {
            // the hit bounds are the entire width of the screen, and the height of the entry
            // with some additional padding above and below.
            return new Rectangle(
                0,
                (int)entry.Position.Y - MenuEntryPadding,
                Owner.ScreenManager.GraphicsDevice.Viewport.Width,
                entry.GetHeight(Owner) + (MenuEntryPadding * 2));
        }
        #region IAnimation Members

        private bool _isStartup = false;
        private TimeSpan _animationTime = TimeSpan.Zero;
        public bool IsStartup
        {
            get { return _isStartup; }
            set { _isStartup = value; }
        }

        public TimeSpan AnimationTime
        {
            get { return _animationTime; }
            set { _animationTime = value; }
        }

        public void Animation(SpriteBatch sp)
        {
            _isStartup = false;
            foreach (var menuEntry in _menuEntries)
            {
                menuEntry.Startup();
            }
        }

        public void Startup()
        {
            _isStartup = true;            
        }

#endregion
    }
}
